package com.us.tag;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;

import com.opensymphony.module.sitemesh.taglib.AbstractTag;
import com.us.util.StringUtil;

/**
 * 集合的标签
 */
@SuppressWarnings("serial")
public class WebDirTag extends AbstractTag {

    private String js;
    private String css;
    private String ext;
    private String val;
    private String excluded;

    @Override
    public int doEndTag() throws JspException {
        try {
            HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
            String base = request.getContextPath();
            if (js != null) {
                base = base + "/common/js/" + getDebugName(js.toString(), excluded);
            }
            if (css != null) {
                base = base + "/common/style/" + css.toString();
            }
            if (ext != null) {
                base = base + "/common/ext/" + getDebugName(ext.toString(), excluded);
            }
            if (val != null) {
                base = base + val;
            }
            getOut().write(base);
        } catch (IOException e) {
        }
        return SKIP_BODY;
    }

    public void setJs(String js) {
        this.js = js;
    }

    public void setCss(String css) {
        this.css = css;
    }

    public void setExt(String ext) {
        this.ext = ext;
    }

    public void setVal(String val) {
        this.val = val;
    }

    public void setExcluded(String excluded) {
        if (StringUtil.isTrimEmpty(excluded)) {
            excluded = "false";
        }
        this.excluded = excluded;
    }

    private String getDebugName(String name, String excluded) {
        if (BaseArgs.DEV_MODEL && "false".equals(excluded)) {
            return "debug_" + name.substring(0, name.length() - 2);
        } else {
            return name;
        }
    }
}
